/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.service.action;

import com.guhesan.querycrypt.QueryCrypt;
import com.myapp.service.bo.ActionResultBO;
import com.myapp.service.dao.BaseHibernateDAO;
import com.myapp.service.dao.DAOFactory;
import com.myapp.util.ReflectUtil;
import com.myapp.util.ResourceBundleUtil;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Transaction;

/**
 *
 * @author toint
 */
public class NewServiceAction extends Action {

    private DAOFactory daoFactory = DAOFactory.getDAOFactory(1);
    ActionResultBO actionResult = null;

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse res) throws Exception {
        String pageForward = "";
        boolean isRedirect = true;
        try {
            BaseHibernateDAO.openThreadSession();

            BaseHibernateDAO.getSession().beginTransaction();

            this.actionResult = new ActionResultBO();
            this.daoFactory.executeMethod(form, req, this.actionResult);

            BaseHibernateDAO.getSession().getTransaction().commit();

            isRedirect = this.actionResult.isRedirect();
            if (this.actionResult.getPageForward() == null) {
//                localActionForward1 = null;
//                jsr 158;
            }

            pageForward = getForwardString(this.actionResult.getPageForward(), req, mapping);
            responseOutput(res);
//            localActionForward1 = new ActionForward(pageForward, isRedirect);
        } catch (Exception ex) {
            try {
                ActionForward localActionForward1;
                ex.printStackTrace();

                req.setAttribute("actionError", ex);

                pageForward = ResourceBundleUtil.getErrorPageForward();

                if ((BaseHibernateDAO.getSession() != null) && (BaseHibernateDAO.getSession().isOpen()) && 
                        (BaseHibernateDAO.getSession().getTransaction().isActive())) {
                    BaseHibernateDAO.getSession().getTransaction().rollback();
                    BaseHibernateDAO.getSession().clear();
                }

//                localActionForward2 = mapping.findForward(pageForward);
//                jsr  28;
            } catch (Exception e) {
                ActionForward localActionForward2;
                e.printStackTrace();
                BaseHibernateDAO.sessionFactory = null;
                throw e;
            }
        } finally {
            if (this.actionResult.getActionLogDAOClassName() != null) {
                try {
                    Class actionLogDAOClass = Class.forName(this.actionResult.getActionLogDAOClassName());
                    if (actionLogDAOClass != null) {
                        Method insertMethod = ReflectUtil.findFirstMethod(actionLogDAOClass, this.actionResult.getInsertActionLogDAOMethodName());

                        if (insertMethod != null) {
                            Transaction logTransaction = BaseHibernateDAO.getSession().beginTransaction();
                            insertMethod.invoke(actionLogDAOClass.newInstance(), new Object[]{this.actionResult.getInsertActionLogDAOMethodArg()});

                            logTransaction.commit();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            }
            BaseHibernateDAO.closeSession();
        }
        return null;
    }

    private String getForwardString(String pageForward, HttpServletRequest req, ActionMapping mapping)
            throws Exception {
        ActionForward actionForward = mapping.findForward(pageForward);

        String result = actionForward.getPath();

        if ((this.actionResult != null) && (this.actionResult.getClassName() != null) && (this.actionResult.getMethodName() != null)) {
            String requestTemp = "className=" + this.actionResult.getClassName() + "&" + "methodName" + "=" + this.actionResult.getMethodName();

            result = result + "?" + QueryCrypt.encrypt(req, requestTemp);
        }

        return result;
    }

    private void responseOutput(HttpServletResponse res)
            throws IOException {
        if (this.actionResult.getOutPutStream() != null) {
            res.setContentType(this.actionResult.getContentType());
            res.setHeader(this.actionResult.getHeaderArg0(), this.actionResult.getHeaderArg1());
            res.setContentLength(this.actionResult.getOutPutStream().size());

            ServletOutputStream os = res.getOutputStream();

            os.write(this.actionResult.getOutPutStream().toByteArray());
            os.close();
            os = null;
        }
    }
}
